home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / GraphicViewers / ViewGif2 / Source / ActivateMenu.m < prev    next >
Text File  |  1991-08-14  |  7KB  |  208 lines

  1. /*****************************************************************************/
  2. /* ActivateMenu.m                                 */
  3. /* implementation file of ActivateMenu class of ViewGif2 application         */
  4. /* This IB Module handles most aspects of an Activate menu             */
  5. /* It also can display a panel to cycle through the menu like a slide show   */
  6. /* This code is mostly borrowed from the Draw example App - Thanks, NeXT!    */
  7. /* January 1990  Carl F. Sutter                             */
  8. /*****************************************************************************/
  9.  
  10. #import "ActivateMenu.h"
  11. #import <appkit/Application.h>        // for NXApp
  12. #import <appkit/Matrix.h>
  13. #import <appkit/MenuCell.h>
  14. #import <appkit/ButtonCell.h>        // for title
  15. #import <string.h>            // for strcpy, strcat
  16.  
  17. @implementation ActivateMenu
  18.  
  19. /*****************************************************************************/
  20. /* new - initialize the activate menu                         */
  21. /*****************************************************************************/
  22. + new
  23.    {
  24.    self = [super new];
  25.    
  26.    /* load nib file defining panel and outlets */
  27.    [NXApp loadNibSection:"ActivateMenu.nib" owner:self];
  28.    
  29.    /* get the activate menu handle */
  30.    if (![self findActivateMenu])
  31.       {
  32.       [self free];
  33.       return( nil );
  34.       }
  35.    /* initialize the list of windows and timer */   
  36.    winList = [List new];
  37.    timer = nil;
  38.    
  39.    return( self );
  40.    } /* new 1/31/90 CFS */
  41.  
  42.  
  43. /*****************************************************************************/
  44. /* findActivateMenu: - find the activate menu item from the main menu id     */
  45. /*****************************************************************************/
  46. - (BOOL)findActivateMenu
  47.    {
  48.    int        count;
  49.    Matrix    *matrix;
  50.    MenuCell    *menuCell;
  51.    const char    *s;
  52.  
  53.    matrix = [[NXApp mainMenu] itemList];
  54.    count = [matrix cellCount];
  55.    while (count--)
  56.       {
  57.       menuCell = [matrix cellAt:count :0];
  58.       s = [menuCell title];
  59.       if (s && !strcmp(s, "Activate"))
  60.          {
  61.      activateMenu = [menuCell target];
  62.          return( YES );
  63.      }
  64.       }
  65.    return( NO );
  66.    } /* findActivateMenu 1/31/90 CFS */
  67.    
  68.    
  69. /*****************************************************************************/
  70. /* outlet initialization methods                            */
  71. /*****************************************************************************/
  72. - setSlideshowPanel:anObject  { slideshowPanel  = anObject;  return( self ); }
  73. - setPeriod:anObject          { period          = anObject;  return( self ); }
  74.   
  75.  
  76. /*****************************************************************************/
  77. /* addDocument: - adds the window to the Activate menu                 */
  78. /*****************************************************************************/
  79. - addDocument:(Window *)winAdd
  80.    {
  81.    List        *cells;
  82.    MenuCell    *menuCell;
  83.  
  84.    /* quit if no window was given */
  85.    if (!winAdd) return( self );
  86.    
  87.    /* get the List of ButtonCells that make up the menu */
  88.    cells = [[activateMenu itemList] cellList];
  89.        
  90.    /* add the new item to the menu */
  91.    menuCell = [activateMenu addItem:[winAdd title]
  92.                             action:@selector(activateDocument:) keyEquivalent:0];
  93.    [menuCell setTarget:self];
  94.    
  95.    /* add the new window to the window list */
  96.    [winList addObject:winAdd];
  97.    
  98.    /* resize the menu matrix to fit the cells and return */
  99.    [activateMenu sizeToFit];
  100.    return( self );
  101.    } /* addDocument: 1/31/90 CFS */
  102.  
  103.  
  104. /*****************************************************************************/
  105. /* removeDocument: - removes the name from the Activate menu             */
  106. /*****************************************************************************/
  107. - removeDocument:(Window *)winRemove
  108.    {
  109.    List        *cells;
  110.    Matrix    *matrix;
  111.    int        i, count;
  112.    const char    *s;
  113.    
  114.    /* quit if no name was given */
  115.    if (!winRemove) return( self );
  116.    
  117.    /* find and remove the named menu item */
  118.    matrix = [activateMenu itemList];
  119.    cells = [matrix cellList];
  120.    count = [cells count];
  121.    for (i=0; i<count; i++)
  122.       {
  123.       s = [[cells objectAt:i] title];
  124.       if (s && !strcmp(s, [winRemove title]))
  125.          {
  126.      [matrix removeRowAt:i andFree:YES];
  127.      //[winRemove close];
  128.          [winList removeObjectAt:i];
  129.      break;
  130.      }
  131.       }
  132.        
  133.    /* resize the menu matrix to fit the cells and return */
  134.    [activateMenu sizeToFit];
  135.    return( self );
  136.    } /* removeDocument: 1/31/90 CFS */
  137.  
  138.  
  139. /*****************************************************************************/
  140. /* start_stop: - toggle the slide show                          */
  141. /*****************************************************************************/
  142. - start_stop:sender
  143.    {
  144.    double    dPeriod;
  145.    
  146.    if (timer == nil)
  147.       {
  148.       nCurrentWindow = 0;
  149.       dPeriod = [period doubleValue];
  150.       [period setEnabled:NO];
  151.       timer = [[Animator alloc] initChronon:dPeriod adaptation:0.0 target:self
  152.                         action:@selector(nextSlide:) autoStart:YES
  153.                     eventMask:NX_ALLEVENTS];
  154.       }
  155.    else  /* stop the current slideshow */ 
  156.       {
  157.       [timer free];
  158.       timer = nil;
  159.       [period setEnabled:YES];
  160.       }                  
  161.    return( self );
  162.    } /* start_stop: 3/7/90 CFS */
  163.  
  164.  
  165. /*****************************************************************************/
  166. /* show: - display the slideshow panel                         */
  167. /*****************************************************************************/
  168. - show:sender
  169.    {
  170.    [slideshowPanel makeKeyAndOrderFront:self];
  171.    return( self );
  172.    } /* show: 3/7/90 CFS */
  173.  
  174.  
  175. /*****************************************************************************/
  176. /* activateDocument: - sent from menu, activate the appropriate window         */
  177. /*****************************************************************************/
  178. - activateDocument:sender
  179.    {
  180.    [[winList objectAt:[sender selectedRow]] makeKeyAndOrderFront:self];
  181.    return( self );
  182.    } /* activateDocument 1/31/90 CFS */
  183.    
  184.  
  185. /*****************************************************************************/
  186. /* windowWillClose: - message from a doc window that it will close         */
  187. /*****************************************************************************/
  188. - windowWillClose:(Window *)winRemove
  189.    {
  190.    [self removeDocument:winRemove];
  191.    return( self );
  192.    } /* windowWillClose: 1/31/90 CFS */
  193.  
  194.  
  195. /*****************************************************************************/
  196. /* nextSlide: - show the next document in the activate menu             */
  197. /*****************************************************************************/
  198. - nextSlide:sender
  199.    {
  200.    nCurrentWindow++;
  201.    if (nCurrentWindow > ([winList count] - 1)) nCurrentWindow = 0;
  202.    [[winList objectAt:nCurrentWindow] makeKeyAndOrderFront:self];
  203.    return( self );
  204.    } /* nextSlide: 3/7/90 CFS */
  205.  
  206.   
  207. @end
  208.